//使用前必须包函有hq funddata.js
pickerContainer = {
    name: [],
    content: []
};
function fundPicker(id, option) {
	id = (!!(id && id.nodeType == 1))?id:document.getElementById(id);
    if (!getFundPicker(id)) {
        pickerContainer.name[pickerContainer.name.length] = id;
        pickerContainer.content[pickerContainer.content.length] = new autoPicker(id, option);
        return pickerContainer.content[pickerContainer.content.length - 1]
    }
	else
	{
		return getFundPicker(id);
	}
}
function getFundPicker(id)
{
	if (pickerContainer == null) pickerContainer = {
        name: [],
        content: []
    };
	id = (!!(id && id.nodeType == 1))?id:document.getElementById(id);
    for (var i = 0; i < pickerContainer.name.length; i++) {
        if (pickerContainer.name[i] == id) return pickerContainer.content[i]
    }
    if (i >= pickerContainer.name.length) {
        return false;
    }
}

function IsWhat(value) {
    if (IsChinese(value)) return {
        Type: 0,
        name: "名称"
    };
    else if (IsNumber(value)) return {
        Type: 1,
        name: "代码"
    };
    else return {
        Type: 2,
        name: "拼音"
    }
}
function IsChinese(value) {
    value = value.charAt(0);
    var escape_v = escape(value);
    if (escape_v.length == (value.length) * 6 || value == "\r" || value == "\n") return true;
    else return false
}
function IsNumber(value) {
    var num = parseInt(value);
    if (num == 0 || num) return true;
    else return false
}
function Trim(value) {
    return value.replace(/^\s+|\s+$/g, "")
}
autoPicker.GetEventFrom = function(e) {
    var ele;
    ele = e.target ? e.target: e.srcElement;
    return ele
}
autoPicker.Bind = function(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function() {
        return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)))
    }
}
autoPicker.BindAsEventListener = function(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function(e) {
        return fun.call(object, (e || window.event), args)
    }
}
autoPicker.attachEventHandle = function(elem, eventName, func) {
    if (window.attachEvent) {
        elem.attachEvent('on' + eventName, func)
    } else if (window.addEventListener) {
        elem.addEventListener(eventName, func, false)
    }
}
autoPicker.detachEventHandle = function(elem, eventName, func) {
    if (window.detachEvent) {
        elem.detachEvent('on' + eventName, func)
    } else if (window.removeEventListener) {
        elem.removeEventListener(eventName, func, false)
    }
}
autoPicker.getPosition = function(elem) {
    var x = 0,
    y = 0;
    while (elem) {
        x += elem.offsetLeft || 0;
        y += elem.offsetTop || 0;
        elem = elem.offsetParent
    }
    return {
        x: x,
        y: y
    }
}
autoPicker.extend = function(destination, source) {
    for (property in source) {
        destination[property] = source[property]
    }
    return destination
}
function autoPicker(id, option,data) {
	this.dataSource=data;
    this.option = autoPicker.extend({
        domDefaultValue: "代码/名称/拼音",
        maxLength: 10,
        listClass: "autoPicker_listDiv",
        listTitleClass: "autoPicker_listTitle",
        selectClassName: "autoPicker_selectClassName",
        viewValue: "name",
        returnValue: "code",
		Data:this.dataSource
    },
    option);
    this.$Dom = ( !! (id && id.nodeType == 1)) ? id: document.getElementById(id);
    this.$Div = this.$Dom.parentNode.insertBefore(document.createElement('div'), this.$Dom);

    this.$Div.className = this.option.listClass;
    with(this.$Div.style) {
        this.$Div.style.zIndex = 9999999;
        this.$Div.style.position = 'absolute';
        this.$Div.style.left = this.$Dom.offsetLeft + 'px';
        this.$Div.style.top = this.$Dom.offsetTop + this.$Dom.offsetHeight + 'px';
        this.$Div.style.display = "none"
    }
	this.divHasFoucs = false;
    this.selectValue = "";
    this.selectData = [];
    this.selectNum = -1;
	this.selectValue=this.$Dom.value;
    this.getData();
	if(this.selectData.length ==0)
	{
		this.option.domDefaultValue = this.$Dom.value;
		this.clearDom();
		this.selectValue = "";
		this.getData();
	}
	
    autoPicker.attachEventHandle(this.$Dom, "focus", autoPicker.Bind(this, this.apFoucs));
    autoPicker.attachEventHandle(this.$Dom, "blur", autoPicker.Bind(this, this.apBlur));
    autoPicker.attachEventHandle(this.$Dom, "click", autoPicker.Bind(this, this.showDiv));
    autoPicker.attachEventHandle(this.$Dom, "keyup", autoPicker.BindAsEventListener(this, this.apKeyUp));
    autoPicker.attachEventHandle(this.$Dom, "keydown", autoPicker.BindAsEventListener(this, this.apKeyDown));
    autoPicker.attachEventHandle(this.$Div, "click", autoPicker.BindAsEventListener(this, this.adClick));
    autoPicker.attachEventHandle(this.$Div, "mouseover", autoPicker.BindAsEventListener(this, this.adMouseOver));
	autoPicker.attachEventHandle(this.$Div, "focus", autoPicker.BindAsEventListener(this, this.divFoucs));
	autoPicker.attachEventHandle(this.$Div, "blur", autoPicker.BindAsEventListener(this, this.divBlur));
	autoPicker.attachEventHandle(this.$Div, "mousedown", autoPicker.BindAsEventListener(this, this.divFoucs));
	autoPicker.attachEventHandle(this.$Div, "mouseup", autoPicker.BindAsEventListener(this, this.divMouseUp));
}
autoPicker.prototype = {
    divFoucs: function () {
        this.divHasFoucs = true;
    },
    divBlur: function () {
        this.divHasFoucs = false;
        this.apBlur();
    },
    divMouseUp: function () {
        this.divHasFoucs = false;
    }
	,
    adClick: function (e) {
        this.divHasFoucs = false;
        this.selectCurrentResult()
    },
    getValue: function () {
        var p = {
            hasValue: false
        }
        p.code = this.$Dom.getAttribute("code");
        p.value = this.$Dom.value;
        if (p.code != "" && p.code != null) p.hasValue = true;
        return p
    },
    setValue: function (value) {
        this.$Dom.value = (value != null) ? value : "";
        this.$Dom.setAttribute("code", "");
        this.$Dom.setAttribute("title", "");
        this.apMust();
        this.setDom()
    },
    adMouseOver: function (e) {
        var tempDom = autoPicker.GetEventFrom(e);
        tempDom = tempDom.parentNode;
        if (tempDom.nodeName.toLowerCase() == "tr") {
            var j;
            for (j = 0; j < tempDom.parentNode.children.length; j++) {
                if (tempDom == tempDom.parentNode.children[j]) break
            }
            if (j < tempDom.parentNode.children.length) {
                this.selectNow(j)
            }
        }
    },
    selectNow: function (i) {
        if (this.selectData.length <= 0)
            return;
        var tempClassName;
        tempClassName = this.$Div.children[1].children[0].children[this.selectNum].className;
        tempClassName = tempClassName.replace(this.option.selectClassName, "");
        this.$Div.children[1].children[0].children[this.selectNum].className = tempClassName;
        this.selectNum = i;
        this.showSelect()
    },
    showDiv: function () {
        this.$Dom.parentNode.insertBefore(this.$Div, this.$Dom);
        this.$Div.style.display = "block";
        var html = '';
        this.selectNum = this.selectData.length > 0 ? 0 : -1;
        for (var i = 0, len = this.selectData.length; i < len; i++) {
            var tempDataName = this.selectData[i][0];
            var tempDataCode = this.selectData[i][1];
            if (this.selectValue != "") {
                var textType = IsWhat(this.selectValue);
                var reg = RegExp(this.selectValue);
                switch (textType.Type) {
                    case 0:
                        tempDataName = this.selectData[i][0].replace(reg, "<b>" + this.selectValue + "</b>");
                        break;
                    case 1:
                        tempDataCode = this.selectData[i][1].replace(reg, "<b>" + this.selectValue + "</b>");
                        break;
                    case 2:
                        var tempPy = this.selectData[i][2].split(",");
                        var tempDataPy = tempPy[tempPy.length - 1].toUpperCase().indexOf(this.selectValue.toUpperCase());
                        tempLength = this.selectValue.length;
                        if (tempDataPy >= 0) tempDataName = tempDataName.substr(0, tempDataPy) + "<b>" + tempDataName.substr(tempDataPy, tempLength) + "</b>" + tempDataName.substr(tempDataPy + tempLength);
                        break
                }
            }
            html += "<tr><td>" + tempDataCode + "</td><td>" + tempDataName + "</td></tr>"
        }
        if (html == '') html = "<div class='" + this.option.listTitleClass + "'>没有 '" + this.selectValue + "' 相关的数据</div>";
        else if (this.selectValue == "") html = "<div class='" + this.option.listTitleClass + "'></div><table cellpadding=0 cellspacing=0>" + html + "</table>";
        else html = "<div class='" + this.option.listTitleClass + "'> " + this.selectValue + " 按 <i>" + IsWhat(this.selectValue).name + "</i> 排序</div><table cellpadding=0 cellspacing=0>" + html + "</table>";
        this.$Div.innerHTML = html;
        this.showSelect();
    },
    showSelect: function () {
        if (this.selectNum < 0) return;
        var tempClassName;
        tempClassName = this.$Div.children[1].children[0].children[this.selectNum].className;
        tempClassName = tempClassName.replace(this.option.selectClassName, "");
        tempClassName = tempClassName + " " + this.option.selectClassName;
        this.$Div.children[1].children[0].children[this.selectNum].className = tempClassName
    },
    hideDiv: function () {
        if (!this.divHasFoucs)
            this.$Dom.parentNode.removeChild(this.$Div);
    },
    clearDom: function () {
        if (this.$Dom.value == Trim(this.option.domDefaultValue)) {
            this.$Dom.value = ""
        }
    },
    setDom: function () {
        if (Trim(this.$Dom.value) == '') {
            this.$Dom.value = this.option.domDefaultValue
        }
    },
    apFoucs: function () {
        this.clearDom();
        this.$Dom.select();
        this.refreshDiv(true);
    },
    apBlur: function () {
        if (!this.divHasFoucs) {
            setTimeout(autoPicker.Bind(this, this.hideDiv), 200);
            this.apMust();
            this.setDom()
        }
    },
    apMust: function () {
        var p = this.getValue();
        if (!p.hasValue && p.value != "") {
            var tempType = IsWhat(p.value);
            var i;
            for (i = 0; i < this.option.Data.length; i++) {
                if (this.option.Data[i][tempType.Type] == p.value) {
                    this.$Dom.title = this.option.Data[i][1];
                    this.$Dom.value = this.option.Data[i][0];
                    this.$Dom.setAttribute("code", this.option.Data[i][1]);
                    this.selectValue = this.$Dom.value;
                    this.getData()
                }
            }
        }
    },
    prevResult: function () {
        if (this.selectNum > 0) this.selectNow(this.selectNum - 1);
        else this.selectNow(this.selectData.length - 1)
    },
    nextResult: function () {
        if (this.selectNum < this.selectData.length - 1) this.selectNow(this.selectNum + 1);
        else this.selectNow(0)
    },
    selectCurrentResult: function () {
        if (this.selectNum >= 0 && this.selectNum < this.selectData.length) {
            this.$Dom.title = this.selectData[this.selectNum][1];
            this.$Dom.value = this.selectData[this.selectNum][0];
            this.$Dom.setAttribute("code", this.selectData[this.selectNum][1]);
            this.selectValue = this.$Dom.value;
            this.getData();
            this.apBlur();
            if (this.option.callBack) this.option.callBack()
        }
    },
    getData: function () {
        this.selectData.length = 0;
        if (this.selectValue == "") {
            if (this.option.hotData) {
                for (j = 0; j < this.option.maxLength && j < this.option.hotData.length; j++) {
                    this.selectData[j] = this.option.hotData[j]
                }
            }
            else {
                for (j = 0; j < this.option.maxLength && j < this.option.Data.length; j++) {
                    this.selectData[j] = this.option.Data[j]
                }
            }
        } else {
            var textType = IsWhat(this.selectValue);
            var j = 0;
            for (i = 0; i < this.option.Data.length && j < this.option.maxLength; i++) {
                var t = this.option.Data[i][textType.Type];
                if (textType.Type == 2) {
                    var x = t.split(",");
                    t = x[x.length - 1];
                }
                if (t.toUpperCase().indexOf(this.selectValue.toUpperCase()) == 0) {
                    this.selectData[j] = this.option.Data[i];
                    j = j + 1
                }
            }
            for (i = 0; i < this.option.Data.length && j < this.option.maxLength; i++) {
                if (this.option.Data[i][textType.Type].toUpperCase().indexOf(this.selectValue.toUpperCase()) > 0) {
                    this.selectData[j] = this.option.Data[i];
                    j = j + 1
                }
            }
        }
    },
	resetData: function (data) {
		
    },
    refreshDiv: function (enb) {
        var temp = Trim(this.$Dom.value);
        if (!enb) enb = false;
        if (this.selectValue != temp || this.selectValue == "") {
            this.selectValue = temp;
            this.getData();
            this.$Dom.title = "";
            this.$Dom.setAttribute("code", "");
            enb = true
        }
        if (enb) this.showDiv()
    },
    apKeyDown: function (e) {
        if (e.keyCode == 13) {
            this.selectCurrentResult();
            if (e.preventDefault) e.preventDefault();
            if (e.stopPropagation) e.stopPropagation();
            if (typeof (e.cancelBubble) !== "undefined") {
                e.cancelBubble = true;
                e.returnValue = false;
            }
        }
    },
    reBind: function(data){
    	this.option.Data = data;
		//getData();
    	//refreshDiv();
    },
    apKeyUp: function (e) {
        if (e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 27) {
            switch (e.keyCode) {
                case 38:
                    this.prevResult();
                    break;
                case 40:
                    this.nextResult();
                    break;
                case 27:
                    this.hideDiv();
                    break
            }
        } else {
            this.refreshDiv(true)
        }
    }
}